package com.stylefeng.guns.gateway.modular.cinema;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.cinema.CinemaServiceApi;
import com.stylefeng.guns.api.cinema.vo.*;
import com.stylefeng.guns.api.order.OrderServiceApi;
import com.stylefeng.guns.gateway.modular.cinema.vo.CinemaConditionResponseVO;
import com.stylefeng.guns.gateway.modular.cinema.vo.CinemaFieldRespomseVO;
import com.stylefeng.guns.gateway.modular.cinema.vo.CinemaFieldsResponseVO;
import com.stylefeng.guns.gateway.modular.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther: madison
 * @date: 2018/11/10 22:47
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/cinema/")
public class CinemaController {

    public static final String IMG_PRE = "http://img.meetingshop.cn";

    @Reference(interfaceClass = CinemaServiceApi.class, check = false, cache = "lru", connections = 10)
    private CinemaServiceApi cinemaServiceApi;

    @Reference(interfaceClass = OrderServiceApi.class, check = false)
    private OrderServiceApi orderServiceApi;

    /**
     * 查询影院列表-根据条件查询所有影院
     * @param cinemaQueryVO
     * @return
     */
    @RequestMapping("getCinemas")
    public ResponseVO getCinemas(CinemaQueryVO cinemaQueryVO){

        try {
            // 按照五个条件进行筛选
            Page<CinemaVO> cinemas = cinemaServiceApi.getCinemas(cinemaQueryVO);
            // 判断是否有满足条件的影院
            if(cinemas.getRecords() == null || cinemas.getRecords().size() == 0){
                return ResponseVO.success("没有影院可查");
            }else{
                return ResponseVO.success(cinemas.getCurrent(),(int)cinemas.getPages(),"",cinemas.getRecords());
            }
        }catch (Exception e){
            // 如果出现异常，应该如何处理
            log.error("获取影院列表异常",e);
            return ResponseVO.serviceFail("查询影院列表异常");
        }
    }

    /**
     * 获取影院的查询条件
     * 1. 热点搜索 -> 放缓存
     * 2. banner
     * @param cinemaQueryVO
     * @return
     */
    @RequestMapping("getCondition")
    public ResponseVO getCondition(CinemaQueryVO cinemaQueryVO){
        try {
            // 获取三个集合，然后封装成一个对象返回即可
            List<BrandVO> brands = cinemaServiceApi.getBrands(cinemaQueryVO.getBrandId());
            List<AreaVO> areas = cinemaServiceApi.getAreas(cinemaQueryVO.getDistrictId());
            List<HallTypeVO> hallTypes = cinemaServiceApi.getHallTypes(cinemaQueryVO.getHallType());

            CinemaConditionResponseVO cinemaConditionResponseVO = new CinemaConditionResponseVO();
            cinemaConditionResponseVO.setBrands(brands);
            cinemaConditionResponseVO.setAreas(areas);
            cinemaConditionResponseVO.setHallTypes(hallTypes);
            return ResponseVO.success(cinemaConditionResponseVO);
        }catch (Exception e){
            // 如果出现异常，应该如何处理
            log.error("获取影院列表查询条件失败",e);
            return ResponseVO.serviceFail("获取影院列表查询条件失败");
        }
    }

    /**
     * 获取播放场次接口
     * @param cinemaId
     * @return
     */
    @RequestMapping("getFields")
    public ResponseVO getFields(Integer cinemaId){
        try {
            CinemaInfoVO cinemaInfo = cinemaServiceApi.getCinemaInfoById(cinemaId);
            List<FilmInfoVO> filmInfos = cinemaServiceApi.getFilmInfoByCinemaId(cinemaId);
            CinemaFieldsResponseVO cinemaFieldsResponseVO = new CinemaFieldsResponseVO();
            cinemaFieldsResponseVO.setCinemaInfoVO(cinemaInfo);
            cinemaFieldsResponseVO.setFilmInfos(filmInfos);
            return ResponseVO.success(IMG_PRE,cinemaFieldsResponseVO);
        }catch (Exception e){
            // 如果出现异常，应该如何处理
            log.error("获取播放场次失败",e);
            return ResponseVO.serviceFail("获取播放场次失败");
        }
    }

    /**
     * 获取场次详细信息接口
     * @param cinemaId
     * @param fieldId
     * @return
     */
    @RequestMapping(value = "getFieldInfo", method = RequestMethod.POST)
    public ResponseVO getFieldInfo(Integer cinemaId, Integer fieldId){
        try {
            HallInfoVO hallInfo = cinemaServiceApi.getFilmFieldInfo(fieldId);
            CinemaInfoVO cinemaInfo = cinemaServiceApi.getCinemaInfoById(cinemaId);
            FilmInfoVO filmInfo = cinemaServiceApi.getFilmInfoByFieldId(fieldId);

            // 造几个销售的假数据
            hallInfo.setSoldSeats(orderServiceApi.getSoldSeatByFieldId(fieldId));

            CinemaFieldRespomseVO cinemaFieldRespomseVO = new CinemaFieldRespomseVO();
            cinemaFieldRespomseVO.setCinemaInfo(cinemaInfo);
            cinemaFieldRespomseVO.setHallInfo(hallInfo);
            cinemaFieldRespomseVO.setFilmInfo(filmInfo);
            return ResponseVO.success(IMG_PRE, cinemaFieldRespomseVO);
        }catch (Exception e){
            // 如果出现异常，应该如何处理
            log.error("获取场次详细信息",e);
            return ResponseVO.serviceFail("获取场次详细信息");
        }
    }
}
